﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SlimCode.Windows.Forms.Mobile;
using System.IO;

namespace SlimCode.SlimPasswords
{
  public partial class BrowseFileScreen : UserControl, IScreen
  {
    private IScreenHost m_host; // = null

    public BrowseFileScreen()
    {
      InitializeComponent();
    }

    #region CurrentFolder PROPERTY

    private string m_currentFolder = @"\";

    public string CurrentFolder
    {
      get { return m_currentFolder; }
      set
      {
        m_currentFolder = Path.GetFullPath( ( value ?? string.Empty ).TrimEnd( '\\', '/' ) + @"\" );
        this.OnCurrentFolderChanged();
      }
    }

    protected virtual void OnCurrentFolderChanged()
    {
      this.UpdateList();
    }

    #endregion

    #region SelectedFile PROPERTY

    public string SelectedFile
    {
      get
      {
        FileView fileView = fileSystemList.SelectedItem as FileView;

        if( fileView == null )
          return string.Empty;

        return fileView.FullName;
      }
    }

    #endregion

    #region SelectedFolder PROPERTY

    public string SelectedFolder
    {
      get
      {
        FolderView folderView = fileSystemList.SelectedItem as FolderView;

        if( folderView == null )
          return string.Empty;

        return folderView.FullName;
      }
    }

    #endregion

    private void UpdateList()
    {
      fileSystemList.SuspendUpdate();

      try
      {
        fileSystemList.Items.Clear();

        if( this.CurrentFolder.Length > 1 )
        {
          fileSystemList.Items.Add( new ParentFolderView( Path.GetDirectoryName( this.CurrentFolder.TrimEnd( '\\', '/' ) ) ) );
        }

        try
        {
          string[] folders = Directory.GetDirectories( this.CurrentFolder );

          Array.Sort<string>( folders );

          foreach( string folder in folders )
          {
            fileSystemList.Items.Add( new FolderView( folder ) );
          }
        }
        catch { }

        try
        {
          string[] files = Directory.GetFiles( this.CurrentFolder );

          Array.Sort<string>( files );

          foreach( string file in files )
          {
            fileSystemList.Items.Add( new FileView( file ) );
          }
        }
        catch { }
      }
      finally
      {
        fileSystemList.ResumeUpdate();
      }
    }

    private void selectMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.OK );
      }
    }

    private void cancelMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.Cancel );
      }
    }

    private void fileSystemList_ItemClick( object sender, SlimCode.Windows.Controls.Mobile.ItemEventArgs args )
    {
      FolderView folderView = args.Item as FolderView;

      if( folderView == null )
      {
        FileView fileView = args.Item as FileView;

        System.Diagnostics.Debug.Assert( fileView != null, "Unknown ItemView." );

        if( fileView != null )
        {
          // Make sure the file is selected.
          fileSystemList.SelectedItem = fileView;

          if( m_host != null )
          {
            m_host.HideScreen( DialogResult.OK );
          }
        }
      }
      else
      {
        this.CurrentFolder = folderView.FullName;
      }
    }

    private void AnyControl_GotFocus( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        Program.ToggleInputPanelIf( m_host.MainForm.MainInputPanel, false );
      }
    }

    #region IScreen Members

    public Control Control
    {
      get { return this; }
    }

    public Control DefaultFocus
    {
      get { return fileSystemList; }
    }

    public MainMenu Menu
    {
      get { return mainMenu; }
    }

    public string Title
    {
      get { return "slimPASSWORDS"; }
    }

    public bool Hiding( DialogResult result )
    {
      return true;
    }

    public void HidingChild( IScreen child, DialogResult result )
    {
    }

    public void Showing( IScreenHost host )
    {
      m_host = host;

      this.UpdateList();
    }

    #endregion
  }
}
